<?php

namespace common\db;

use Yii;
use yii\db\Expression;

/**
 * ActiveQuery represents a DB query associated with an Active Record class.
 *
 * @see ActiveRecord
 * @author emhome<emhome@163.com>
 * @since 2.0
 */
class ActiveQuery extends \yii\db\ActiveQuery {

    use QueryDistanceTrait;

    /**
     * Sets the active part of the query.
     *
     * @param bool|string|integer $state
     * @return $this the query object itself
     * @throws \yii\base\InvalidConfigException
     * @see andWhere()
     */
    public function active($state = false) {
        $modelClass = $this->modelClass;
        $model = $modelClass::instance();
        $alis = key($this->getTablesUsedInFrom()) . '.';
        if ($model->hasAttribute('deleted_at')) {
            $this->andWhere([$alis . 'deleted_at' => 0]);
        }
        if ($state !== false && $model->hasAttribute('status')) {
            $this->andWhere([$alis . 'status' => $state]);
        }
        return $this;
    }

    /**
     * @param $primaryColumns
     * @param $withColumns
     * @param string $defaultOperator
     * @return $this
     * @throws \yii\base\InvalidConfigException
     */
    public function andCompareColumns($primaryColumns, $withColumns, $defaultOperator = '=') {
        $tableAlias = key($this->getTablesUsedInFrom()) . '.';
        if (is_string($primaryColumns) && is_string($withColumns)) {
            if (strpos($primaryColumns, '.') === false) {
                $primaryColumns = $tableAlias . $primaryColumns;
            }
            if (strpos($withColumns, '.') === false) {
                $withColumns = $tableAlias . $withColumns;
            }
            $ex = new Expression($primaryColumns . $defaultOperator . $withColumns);
            return $this->andWhere($ex);
        }
        $conditions = [];
        if (is_array($primaryColumns) && is_string($withColumns)) {
            if (strpos($withColumns, '.') === false) {
                $withColumns = $tableAlias . $withColumns;
            }
            foreach ($primaryColumns as $primary) {
                if (strpos($primary, '.') === false) {
                    $primary = $tableAlias . $primary;
                }
                $conditions[] = new Expression($primary . $defaultOperator . $withColumns);
            }
        } elseif (is_string($primaryColumns) && is_array($withColumns)) {
            if (strpos($primaryColumns, '.') === false) {
                $primaryColumns = $tableAlias . $primaryColumns;
            }
            foreach ($withColumns as $with) {
                if (strpos($with, '.') === false) {
                    $with = $tableAlias . $with;
                }
                $conditions[] = new Expression($primaryColumns . $defaultOperator . $with);
            }
        }
        if (!empty($conditions)) {
            array_unshift($conditions, 'OR');
            return $this->andFilterWhere($conditions);
        }
        return $this;
    }

    /**
     * @return string
     */
    public function debug() {
        return $this->createCommand()->rawSql;
    }

}
